﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong
{
    class Ball
    {
        Texture2D texture;
        Vector2 position;
        Rectangle size;
        Vector2 velocity;
        Random random = new Random();
        SoundEffect soundEffect;

        public Ball( GameWindow janela , Texture2D texture, Vector2 position, SoundEffect soundEffect)//<CONSTRUTOR!!!
        {
            this.soundEffect = soundEffect;
            this.texture = texture;//AQUI
            this.size = texture.Bounds;//AQUI
            this.position = new Vector2(
                janela.ClientBounds.Width/2
                - this.size.Width/2 ,
                janela.ClientBounds.Height/2
                - this.size.Height/2);//AQUI

            float randomX = (random.Next(-5,5));
            float randomY = (random.Next(-3,3));
            if (randomX > 0) randomX += 2;
            else randomX -= 2;
            if (randomY > 0) randomY += 2;
            else randomY -= 2;

            this.velocity = new Vector2(randomX,randomY);//AQUI
            
        }

        public void Update (GameWindow janela)
        {
                  position+=velocity;//AQUI

                  manterNaTela(janela);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);

        }

        public void verificaColisao(Pallete palheta)
        {
            if ((position.X > palheta.position.X - size.Width) & (position.X < palheta.position.X + palheta.size.Width) ){
                if ((position.Y > palheta.position.Y) & (position.Y < palheta.position.Y + palheta.size.Height))
                {
                    soundEffect.Play();
                    if (velocity.X > 0)
                    {
                        this.position.X = palheta.position.X - this.size.Width;
                        this.velocity.X += 1;
                    }
                    else
                    {
                        this.position.X = palheta.position.X + palheta.size.Width - this.size.X;
                        this.velocity.X -= 1;
                    }
                    this.velocity.X = -this.velocity.X;
                }
            }
        }

        private void manterNaTela(GameWindow janela)
        {
            if (position.Y <= 0)
            {
                soundEffect.Play();
                position.Y = 0;
                velocity.Y = -velocity.Y;
            }

            if (position.Y >= janela.ClientBounds.Height - size.Height)
            {
                soundEffect.Play();
                position.Y = janela.ClientBounds.Height - size.Height;
                velocity.Y = -velocity.Y;
            }

            if (position.X <= 0)
            {
                soundEffect.Play();
                position.X = 0;
                velocity.X = -velocity.X;
            }

            if (position.X >= janela.ClientBounds.Width - size.Width)
            {
                soundEffect.Play();
                position.X = janela.ClientBounds.Width - size.Width;
                velocity.X = -velocity.X;
            }

        }

        
    }

}
